<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set('memory_limit', '1000M');
set_time_limit(0);
CONST TYPE_NORMAL = 1;
CONST TYPE_BAD = 2;
include_once('function.php');
include_once('sell.php');
include_once('rent.php');
$city=$argv[1];
$borough_file_name = $city.'/borough';
#echo $borough_file_name;die;
$borough_file=file($borough_file_name);

function outputBadHouse($house_info, $bad_type){
    $house_info['bad_type'] = $bad_type;
	    $out = json_encode($house_info);
	}
	/** curl 获取 https post请求
	 * @param String $url 请求的url
	 * @param Array $data 要发送的数据
	 * @param Array $header 请求时发送的header
	 * @param int $timeout 超时时间，默认30s
	 */
	function curlHttpsPost($url, $data=array(), $header=array(), $timeout=5){
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
	    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
	    curl_setopt($ch, CURLOPT_URL, $url);
	    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	    curl_setopt($ch, CURLOPT_POST, true);
	    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

	    $response = curl_exec($ch);

	    if($error=curl_error($ch)){
		#die($error);
	       return '';
	    }

	    curl_close($ch);

	    return $response;

	}

	$borough = array();
	foreach($borough_file as &$line_json){
	    $line = json_decode($line_json, True);
	    $borough[$line['borough_name']] = 
		array('id'=>$line['id'], 'cityarea_id'=>$line['cityarea_id'], 'cityarea2_id'=>$line['cityarea2_id']);
	}
	$stat_array=array();
	$source_array=array();
	$url="http://data.zhugefang.com/Borough/api/insertSimName";
	$list=array();
	$count=1;
	$borough_list=array();
	while (($line_json = fgets(STDIN)) !== false) {
	    $house_info['borough_name']=trim($house_info['borough_name']);
	    $house_info['source_url'] = trim($house_info['source_url']);
    $borough_array=array();
    $line_json = trim($line_json);
    if(empty($line_json))
        continue;
    $house_info = json_decode($line_json, true);
    $house_info['tag'] = str_replace("12#", "", $house_info['tag']);
    $house_info['borough_name'] = trim($house_info['borough_name']);
    if (strstr($house_info['borough_name'], "</span")){
        $house_info['borough_name']=str_replace("</span", "",$house_info['borough_name']);
    }
    if (strstr($house_info['borough_name'], "zhaopin")or strstr($house_info['borough_name'], "%å%")){
        continue;
    }
    
    //处理小区
    if ($borough[$house_info['borough_name']]){
	    $house_info['borough_id'] = $borough[$house_info['borough_name']]['id'];
        $house_info['cityarea_id'] = $borough[$house_info['borough_name']]['cityarea_id'];
        $house_info['cityarea2_id'] = $borough[$house_info['borough_name']]['cityarea2_id'];
    }
    else{
            if ($house_info['borough_name'] == '' || empty($house_info['borough_name']) || $house_info['borough_name'] ==NULL){
                #echo $house_info['source_url']."\n";
                continue;
            }
            if (array_key_exists($house_info['source'], $stat_array)) {
                $borough_array=$source_array[$house_info['source']];
                if (!in_array($house_info['borough_name'], $borough_array)){
                    $stat_array[$house_info['source']] = $stat_array[$house_info['source']] + 1;
                    $borough_array[]=$house_info['borough_name'];
                    $source_array[$house_info['source']]=$borough_array;
                }
            }
            else{
                $stat_array[$house_info['source']] = 1;
                $borough_array=array($house_info['borough_name']);
                $source_array[$house_info['source']]=$borough_array; 
            }
echo $house_info['borough_name']."\n";
            #if ($house_info['source'] != 10){
                #echo $house_info['source']."\n";
            #$url="http://data.zhugefang.com/Borough/api/insertSimName?word=".$house_info['borough_name']."&cityarea_name=".$house_info['cityarea_name']."&cityarea2_name=".$house_info['cityarea2_name']."&city=".$city."&house_url=".$house_info['source_url'];
            $url="http://data.zhugefang.com/Borough/api/insertSimName";
            if (in_array($house_info['borough_name'], $borough_list)){
                continue;
            }
            else{
                #echo $house_info['borough_name']."&&&&&&".$house_info['source_name']."\n";
                echo $house_info['borough_name']."\n";
                $borough_list[]=$house_info['borough_name'];
                $data=array(
                    "word"=>$house_info['borough_name'],
                    "cityarea_name"=>$house_info['cityarea_id'],
                    "cityarea2_name"=>$house_info['cityarea2_id'],
                    "house_url"=>$house_info['source_url']
                );
                #var_dump($data);
                #echo '\n';
                $list[]=$data;
                $count=$count+1;
                if ($count==1000){
                    $list_str=json_encode($list);
                    $para=array(
                        "city"=>$city,
                        "list"=>$list_str
                    );
                    $count=0;
                    $list=array();
//                    curlHttpsPost($url, $para);
                }
            }
        }
}

if ($count>0){
    $list_str=json_encode($list);
    $para=array(
        "city"=>$city,
        "list"=>$list_str
    );
//    curlHttpsPost($url, $para);
//    echo $response."\n";
}
$sum=0;
#echo count($borough_list)."\n";
#foreach ($borough_list as $borough){
    #echo $borough."\n";
#}
#foreach ($stat_array as $key=>$value){
#    echo $value."\t".$key."\n";
#    $sum=$sum + $value;
#}
#echo "sum=".$sum."\n";
    

    
